package step2.question;

import java.util.Scanner;

public class Library {

    public static final int SIZE = 100; //图书库最大藏书量
    Book[] booklist; //图书库
    int count;		//当前图书库的图书数量
    Scanner scan;

    public Library() {
        booklist = new Book[SIZE];
        //初始化图书库，默认里面有5本书，信息如下
        booklist[0] = new Book("Java编程思想", "埃克尔", "机械工业出版社", "9787111213826", 78.0);
        booklist[1] = new Book("C++程序设计", "陈可", "电子工业出版社",  "9787145513853", 45.6);
        booklist[2] = new Book("程序设计基础", "张欣易", "北京大学出版社", "9784380850350",35.0);
        booklist[3] = new Book("Linux入门 ", "李旺", "机械工业出版社", "9784254465775",56.7);
        booklist[4] = new Book("Unix编程大全", "程查斯", "清华大学出版社", "9784343465729",128.5);

        count = 5;
        scan = new Scanner(System.in);
    }

    public void run(){

        int userChoice = 0;
        //循环处理，直到用户选择了“0...退出”
        do{
            displayMenu();

            //读取用户输入
            userChoice = scan.nextInt();

            switch(userChoice){
                case 0:
                    System.out.println("成功退出系统，欢迎再次使用！");
                    break;
                case 1:
                    printAllBook();
                    break;
                case 2:
                    addBook();
                    break;
                case 3:
                    deleteBook();
                    break;
                case 4:
                    modifyBook(); 	//本次任务不要求实现
                    break;
                case 5:
                    findBook();		//本次任务不要求实现
                    break;
                default:
                    System.out.println("输入非法,请重新输入！");

            }
        }while(userChoice != 0);
        scan.close();
    }

    void displayMenu(){

        //打印菜单
        System.out.println("---------------------");
        System.out.println("    图书管理系统     ");
        System.out.println("---------------------");
        System.out.println("|   0...退出系统	|");
        System.out.println("|   1...显示图书	|");
        System.out.println("|   2...增加图书	|");
        System.out.println("|   3...删除图书	|");
        System.out.println("|   4...修改图书	|");
        System.out.println("|   5...查询图书	|");
        System.out.println("---------------------");
        System.out.print("请输入选项：");


    }

    void printAllBook(){ //循环打印所有的图书
        System.out.println("---------------------------------------------------------------------------");
        System.out.println("序号\t书名\t\t作者\t出版社\t\tISBN\t\t单价");
        System.out.println("---------------------------------------------------------------------------");
        //***********Begin************
        //输出booklist数组中count本图书的信息，记得前面需要有个序号（序号从1开始）
        //每项信息请用"\t"隔开
        for(int i = 0;i<count;i++)
        {
            System.out.println((i + 1) + "\t"+booklist[i].getTitle()+"\t"+booklist[i].getAuthor()+"\t"+booklist[i].getPress() + "\t"+booklist[i].getISBN()+"\t"+booklist[i].getPrice());
        }
        //************End*************
        System.out.println("---------------------------------------------------------------------------");
    }

    void addBook(){  //增加图书
        if (count < SIZE){
            System.out.println("----------------------------------------------");

            System.out.print("请输入图书名：");
            String title = scan.next();

            //***********Begin************
            //仿造上两行代码，提示用户输入各项信息
            //创建一个图书对象，加入到booklist数组中
            System.out.print("请输入作者：");
            String author = scan.next();
            System.out.print("请输入出版社：");
            String press = scan.next();
            System.out.print("请输入ISBN：");
            String ISBN = scan.next();
            System.out.print("请输入单价：");
            double price = scan.nextDouble();
            booklist[count] = new Book(title,author,press,ISBN,price);


            //************End*************);
            count++;
            System.out.println("成功增加1本图书！当前图书库信息如下：");
            printAllBook();
        }
        else{
            System.out.println("图书库已满！");
        }
    }

    void deleteBook(){   //删除图书

        int id = -1;
        while(true){
            printAllBook();
            System.out.print("请输入序号删除图书，输入0返回主菜单: ");
            int userChoice = scan.nextInt();
            if(userChoice == 0){ //输入0跳出循环，即返回主菜单
                break;
            }
            else {
                //***********Begin************
                //删除用户选择的图书，并输出"删除成功！"
                //若输入有误，请提示："输入错误！请重新输入！"
                id = findByOrder(userChoice);
                if(id!=-1)
                {
                    for(int i = id;i<count-1;i++)
                    {
                        booklist[i] = booklist[i+1];
                    }
                    count--;
                    System.out.println("删除成功！");
                }
                else
                {
                    System.out.println("输入错误！请重新输入！");
                }

                //************End*************);
            }
        }
    }

    int findByOrder(int number){	//按序号(1~count)返回数组下标id
        if(number > 0 && number <= count ){
            return number - 1;
        }
        else
            return -1;
    }

    void modifyBook() {   //修改图书
        System.out.println("功能建设中...");
    }

    void findBook(){ //查询图书
        System.out.println("功能建设中...");
    }

    public static void main(String[] args) {
        Library myLibrary = new Library();
        //***********Begin************
        //调用run()方法
        myLibrary.run();
        //************End*************);
    }

}